#include <stdio.h>
#include "atmi.h"		/* TUXEDO  Header File */

main(int argc, char *argv[])
{

	char *sendbuf, *rcvbuf;
	long sendlen, rcvlen;
	int ret;
	FILE *fp_in, *fp_out;
	char in_file_name [1024];
	char out_file_name [1024];
	char svc_name [1024];
	char tgt_name [1024];

	char tmp_str [1024 * 4];

	int read_size;
#if 0
	strcpy( svc_name, "B2K2");
	sendlen = 1024;
	rcvlen 	= 4096;
 
	if (tpinit((TPINIT *) NULL) == -1) {
		(void) fprintf(stderr, "Tpinit failed[%s]\n", tpstrerror(tperrno));
		exit(1);
	}
	
	sendlen = 1024;

	/* Allocate STRING buffers for the request and the reply */


	if((sendbuf = (char *) tpalloc("CARRAY", NULL, sendlen+1)) == NULL) {
		(void) fprintf(stderr,"Error allocating send buffer\n");
		tpterm();
		exit(1);
	}

	if((rcvbuf = (char *) tpalloc("CARRAY", NULL, rcvlen+1)) == NULL) {
		(void) fprintf(stderr,"Error allocating receive buffer\n");
		tpfree(sendbuf);
		tpterm();
		exit(1);
	}

	sprintf(in_file_name, "data/%s.txt", svc_name);
	sprintf(out_file_name, "data/%s.out", svc_name);
printf(">%s<\n", in_file_name);
	/* ------------------------------------------------------------------ */
#if 1
	fp_in = fopen( in_file_name, "rb" );
	fp_out = fopen( out_file_name, "wb" );

	if(fp_in == NULL){
		fprintf(stderr,"input file open err\n");
		exit(0);
	}
	if(fp_out == NULL){
		fprintf(stderr,"output file open err\n");
		exit(0);
	}

	read_size = fread((char *)tmp_str, 1, (int)1024, (FILE *)fp_in);
	
#endif


  fprintf( stderr, ">SVC_NAME: %s<\n", svc_name);
	/* ------------------------------------------------------------------ */
  fprintf( stderr, ">%s(%d)<\n", tmp_str, read_size);
	HexLog( tmp_str, read_size);

	memset( sendbuf, 0x00, 8);
	strcpy( sendbuf,  svc_name);
	Ascii2EbcdicStr( tmp_str, sendbuf + 8, read_size - 1);

	sendlen = 8 + read_size - 1;
	HexLog( sendbuf, sendlen);

#if 1
	/* ------------------------------------------------------------------ */
	ret = tpcall("EAI2HOST", (char *)sendbuf, sendlen, (char **)&rcvbuf, &rcvlen, (long)0);
	if(ret == -1) {
		(void) fprintf(stderr, "Can't send request to service(%s)\n",tpstrerror(tperrno));
		tpfree(sendbuf);
		tpfree(rcvbuf);
		tpterm();
		exit(1);
	}
#endif

	fprintf( stderr, ">rcvlen: %d<\n", rcvlen);
	HexLog( rcvbuf, rcvlen);

	Ebcdic2AsciiStr( rcvbuf, tmp_str, rcvlen);
	printf("rcv[%d]:%s\n", rcvlen, tmp_str);

	fwrite((char *)rcvbuf, 1, rcvlen, (FILE *)fp_out);

	if(fp_in) fclose(fp_in);
	if(fp_out) fclose(fp_out);

	tpfree(sendbuf);
	tpfree(rcvbuf);
	tpterm();
#endif
	return(0);
}
